package mapper;

import model.Bill;
import model.MyObjectOutputStream;

import java.io.*;
import java.util.LinkedList;

/**
 * @InterfaceName: TransferFile
 * @Description:处理账单传递到文件或者从文件取出
 * @Author: DaDaGuai
 * @Date: 2024/12/24
 */
public class TransferFile {
    private static String filePath = "untitled/bills";//文件的相对路经
    private static String filePathMoney = "untitled/money";//金额的相对路经
    private static String fileLimitMoney = "untitled/limitMoney";//限额的相对路经
    private static String fileExpenses = "untitled/expenses";//支出的相对路经
    private static String fileIncome = "untitled/expenses";//支出的相对路经

    /**
    *@Description: 账单传入文件
    *@Author: DaDaGuai
    *@Date:
    */
    public static Boolean RecordIincomeMapper(Bill bill){

        File file = new File(filePath);
        boolean append = file.exists();

        try (ObjectOutputStream oos = append ?
                new MyObjectOutputStream(new FileOutputStream(filePath, true)) :
                new ObjectOutputStream(new FileOutputStream(filePath))) {

            oos.writeObject(bill);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
    *@Description:读取全部账单
    *@Author: DaDaGuai
    *@Date:
    */
    public static LinkedList<Bill> readIncomeMapper() {
        LinkedList<Bill> billList = new LinkedList<>();  // 用 LinkedList 来存储 Bill 对象
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            while (true) {
                try {
                    // 读取一个对象
                    Object obj = ois.readObject();
                    // 判断对象类型并进行相应处理
                    if (obj instanceof Bill) {
                        Bill bill = (Bill) obj;
                        billList.add(bill);  // 将 Bill 对象添加到链表中
                    }
                } catch (EOFException e) {
                    // 文件结束，退出循环
                    break;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return billList;  // 返回存储 Bill 对象的链表
    }


    /**
    *@Description:费用存储文件
    *@Author: DaDaGuai
    *@Date:
    */
    public static boolean saveNumber(double number) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileLimitMoney))) {
            writer.write(String.valueOf(number));  // 写入数字
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
    *@Description:费用取出文件
    *@Author: DaDaGuai
    *@Date:
    */
    public static double loadNumber() {
        double number = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(filePathMoney))) {
            String line = reader.readLine();
            if (line != null) {
                number = Double.parseDouble(line);  // 解析文件中的数字
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }
    /**
    *@Description:输入限额
    *@Author: DaDaGuai
    *@Date:
    */
    public static boolean inputLimitMoney(double number) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileLimitMoney))) {
            writer.write(String.valueOf(number));  // 写入数字
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     *@Description:得到限额
     *@Author: DaDaGuai
     *@Date:
     */
    public static double getLimitMoney() {
        double number = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(fileLimitMoney))) {
            String line = reader.readLine();
            if (line != null) {
                number = Double.parseDouble(line);  // 解析文件中的数字
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }
    /**
     *@Description:输入支出的金额
     *@Author: DaDaGuai
     *@Date:
     */
    public static boolean inputExpenses(double number) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileExpenses))) {
            writer.write(String.valueOf(number));  // 写入数字
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     *@Description:得到支出的金额
     *@Author: DaDaGuai
     *@Date:
     */
    public static double getExpenses() {
        double number = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(fileExpenses))) {
            String line = reader.readLine();
            if (line != null) {
                number = Double.parseDouble(line);  // 解析文件中的数字
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }
    /**
     *@Description:输入收入的金额
     *@Author: DaDaGuai
     *@Date:
     */
    public static boolean inputIncome(double number) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileIncome))) {
            writer.write(String.valueOf(number));  // 写入数字
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     *@Description:得到收入的金额
     *@Author: DaDaGuai
     *@Date:
     */
    public static double getIncome() {
        double number = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(fileIncome))) {
            String line = reader.readLine();
            if (line != null) {
                number = Double.parseDouble(line);  // 解析文件中的数字
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }



}
